package com.yinhe.music.yhmusic.collect;

import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.base.IBaseView;

public class CollectPresenter<M extends IBaseModel, V extends IBaseView> extends BasePresenter<M, V> implements CollectContract.ICollectPresenter {

    @Override
    public void collect(int id, String collectType, int collectStatues) {
        CollectContract.ICollectView mView = (CollectContract.ICollectView) getView();
        if (mView == null) {
            return;
        }

        mView.showLoading();

        if (collectStatues == 0) {
            addSubscription(mModel.collect(id, collectType)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        getView().showToast(baseInfoResponse.getMsg());
                        mView.updateCollectUI();
                        mView.hideLoading();
                    }, throwable -> {
                        getView().showMessage(throwable);
                        mView.hideLoading();
                        mView.showMessage(throwable);
                    }));
        } else {
            addSubscription(mModel.unCollect(id, collectType)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        getView().showToast(baseInfoResponse.getMsg());
                        mView.updateCollectUI();
                        mView.hideLoading();
                    }, throwable -> {
                        getView().showMessage(throwable);
                        mView.hideLoading();
                        mView.showMessage(throwable);
                    }));
        }
    }
}
